﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.Repository.Domain;
using OpenAuth.App;

using Infrastructure;

namespace OpenAuth.WebApi.Areas.HisApi.Controllers
{
    /// <summary>
    /// 门诊病人信息
    /// </summary>
    [Route("HisApi/[controller]/[action]")]
    [ApiController]
    public class PatiendInfoController : ControllerBase
    {
        /// <summary>
        /// 查询门诊患者基本信息
        /// </summary>
        private readonly CMZXX001App cmzxx001App;
        /// <summary>
        /// 首诊患者建档
        /// </summary>
        private readonly BMZXX001App bmzxx001App;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_cmzxx001App"></param>
        /// <param name="_bmzxx001App"></param>
        public PatiendInfoController(CMZXX001App _cmzxx001App, BMZXX001App _bmzxx001App)
        {
            cmzxx001App = _cmzxx001App;
            bmzxx001App = _bmzxx001App;
        }

        /// <summary>
        /// 查询门诊患者基本信息(证件号)
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryPatientByID(string ID)
        {
            var result = new Response<List<CMZXX001>>();
            //查询门诊患者基本信息
            var queryResult = cmzxx001App.QueryCMZXX001ByID(ID);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return (new { success = "true", requestId = Guid.NewGuid().ToString(), patinfos = result.Result.First() }).ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }

        /// <summary>
        /// 查询门诊患者基本信息(院内卡)
        /// </summary>
        /// <param name="Card"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCMZXX001ByCard(string Card)
        {
            var result = new Response<List<CMZXX001>>();
            //查询门诊患者基本信息
            var queryResult = cmzxx001App.QueryCMZXX001ByCard(Card);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return (new { success = "true", requestId = Guid.NewGuid().ToString(), patinfos = result.Result.First() }).ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }

        /// <summary>
        /// 查询门诊患者基本信息(电话号)
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCMZXX001ByPhone(string Phone)
        {
            var result = new Response<List<CMZXX001>>();
            //查询门诊患者基本信息
            var queryResult = cmzxx001App.QueryCMZXX001ByPhone(Phone);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return (new { success = "true", requestId = Guid.NewGuid().ToString(), patinfos = result.Result.First() }).ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }

        /// <summary>
        /// 查询门诊患者基本信息(patid)
        /// </summary>
        /// <param name="ID">patid</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCMZXX005ByPID(string patid, string name)
        {
            var result = new Response<List<CMZXX001>>();
            //查询门诊患者基本信息
            var queryResult = cmzxx001App.QueryCMZXX005ByPID(patid, name);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return (new { success = "true", requestId = Guid.NewGuid().ToString(), patinfos = result.Result.First() }).ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }

        /// <summary>
        /// 首诊患者建档
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>    
        [HttpGet]
        public string PutBMZXX001(string hzxm,string sex,string zjh,string birth,string lxdz,string lxdh,string memo,string lxrxm,string lxrdh,string lxrzjh,string zydm,string mzdm)
        {
            BMZXX001 model = new BMZXX001();
            model.hzxm = hzxm;
            model.sex = sex;
            model.zjh = zjh;
            model.birth = birth;
            model.lxdz = lxdz;
            model.lxdh = lxdh;
            model.memo = memo;
            model.lxrxm = lxrxm;
            model.lxrdh = lxrdh;
            model.lxrzjh = zjh;
            model.zydm = zydm;
            model.mzdm = mzdm;

            var result = new Response<List<BMZXX001Response>>();
            var PutResult = bmzxx001App.PutBMZXX001(model);
            result.Result = PutResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    patinfos = new { result.Result.First().patid, result.Result.First().blh, message = "" }
                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
    }
}